package cky_interface;

import java.awt.*;
import java.io.File;

import javax.swing.*;

import cky_parser.CKYTable;
import cky_parser.Grammar;
import cky_interface.iTable;;

public class MainFrame extends JFrame{
	
	Grammar grm;			//
	CKYTable tab;			//
	JPanel declarePanel;	// Information Panel
	JLabel lb;				//
	JPanel centerPanel;		// Table panel
	JButton btnNext;		//
	JButton btnBack;		//
	JPanel bottomPanel;		// Panel for Play button, Stop button and status label
	JButton btnPlay;		//
	JButton btnStop;		//
	JLabel lbStatus;		//
	java.util.Timer timer;  //
	String grammar;
	String dictionary;
	String querySentence;
	
	public void InitializeComponent(){
		String currentDir = System.getProperty("java.class.path");
		File i = new File(currentDir);
		//String opSeparate;
		//if(currentDir.charAt(0) == '/'){
		//	opSeparate= "/";			
		//}
		//else{
		//	opSeparate= "\\";	
		//}
			
		grammar = i.getParent()+ File.separator+"Grammar";
		dictionary = i.getParent()+ File.separator+"Dictionary";
		querySentence = "the chef eats fish with the chopsticks";
		
		SetupTable(grammar,dictionary,querySentence);
		
		// Add Declaration Panel
		declarePanel = new JPanel();
		
		declarePanel.setLayout(new GridLayout(3,0));
		

		JLabel lb;
		JTextField tf;
		JPanel pn;
		pn = new JPanel();
		lb = new JLabel("Grammar:");		
		declarePanel.add(lb);	
		tf = new JTextField(grammar);
		tf.setEnabled(false);
		declarePanel.add(tf);
		declarePanel.add(pn);
		
		pn = new JPanel();	
		lb = new JLabel("Dictionary:");
		declarePanel.add(lb);
		tf = new JTextField(dictionary);
		tf.setEnabled(false);
		declarePanel.add(tf);	
		declarePanel.add(pn);
		
		pn = new JPanel();
		lb = new JLabel("Query Sentence:");
		declarePanel.add(lb);
		tf = new JTextField(querySentence);
		tf.setEnabled(false);
		declarePanel.add(tf);	
		declarePanel.add(pn);
		
		declarePanel.setBackground(Color.LIGHT_GRAY);
		
		this.add(declarePanel,BorderLayout.NORTH);
		
		// Add Back button
		btnBack = new JButton("Back");
		btnBack.addActionListener(new MainFrameButtonEvent(this, "Back"));
		this.add(btnBack,BorderLayout.WEST);
		// Add Next button
		btnNext = new JButton("Next");
		btnNext.addActionListener(new MainFrameButtonEvent(this, "Next"));
		this.add(btnNext,BorderLayout.EAST);
		
		
		iTable table = new iTable(tab.words.length,tab.words.length, tab.words);
		table.MatchTable(table, tab);
		this.centerPanel = table;
		this.add(centerPanel);
		

		
		bottomPanel = new JPanel();
		bottomPanel.setLayout(new GridLayout(2,1));
		// Add label status
		lbStatus = new JLabel(" ",JLabel.CENTER);
		lbStatus.setOpaque(true);
		lbStatus.setBackground(Color.blue);
		lbStatus.setForeground(Color.white);
		bottomPanel.add(lbStatus);
		

		pn = new JPanel();
		// Add Play button
		btnPlay = new JButton("Play");
		btnPlay.addActionListener(new MainFrameButtonEvent(this, "Play"));
		pn.add(btnPlay);
		
		
		// Add Stop button
		btnStop = new JButton("Stop");
		btnStop.addActionListener(new MainFrameButtonEvent(this, "Stop"));
		pn.add(btnStop);
		
		bottomPanel.add(pn);
		
		
		this.add(bottomPanel,BorderLayout.SOUTH);
		

	}
	public void SetupTable(String grammar,String dictionary, String querySentence){
		grm = new Grammar (grammar, dictionary);
		tab = new CKYTable(querySentence, grm);
		tab.Process();		
	}
	public void NextBtnPerformed(){
		if (tab.totalOrderIndex < tab.phraseList.size()){
			try {
				this.tab.next();
				this.remove(centerPanel);
				iTable table = new iTable(tab.words.length,tab.words.length, tab.words);
				table.MatchTable(table, tab);
				this.centerPanel = table;
				this.add(centerPanel);
				this.lbStatus.setText(tab.statusLabel);
				this.paintAll(this.centerPanel.getGraphics());
				this.invalidate();
			}
			catch (Exception e){
			}
		}
	}
	public void BackBtnPerformed(){
		if (tab.totalOrderIndex > 1){
			try {
				this.tab.back();
				this.remove(centerPanel);
				iTable table = new iTable(tab.words.length,tab.words.length,tab.words);
				table.MatchTable(table, tab);
				this.centerPanel = table;
				this.add(centerPanel);
				this.lbStatus.setText(tab.statusLabel);
				this.paintAll(this.centerPanel.getGraphics());
				this.invalidate();
			}
			catch (Exception e){
			}
		}
	}
	public void PlayBtnPerformed(){
		try {
			SetupTable(grammar,dictionary,querySentence);
			timer = new java.util.Timer();
			MainFrameTimerTask task = new MainFrameTimerTask(this);
			timer.schedule(task, 0, 2000);
			SetEnableButton(false);
		}
		catch (Exception e){
		}
	}
	public void StopBtnPerformed(){
		try {
			timer.cancel();
			SetEnableButton(true);
		}
		catch (Exception e)
		{}
	}
	
	public void SetEnableButton(boolean isEnable){
		if(isEnable){
			this.btnBack.setEnabled(true);
			this.btnNext.setEnabled(true);
			this.btnPlay.setEnabled(true);
			this.btnStop.setEnabled(false);
		}
		else{

			this.btnBack.setEnabled(false);
			this.btnNext.setEnabled(false);
			this.btnPlay.setEnabled(false);
			this.btnStop.setEnabled(true);
		}
	}
}


